<?php
include_once 'db/li.php';

@$status = $_REQUEST['status'];
@$noPayOrderId = $_REQUEST['noPayOrderId'];
include 'header.php';

@$pageNum = $_REQUEST['pageNum'];
if(empty($pageNum)||$pageNum < 1||!is_numeric($pageNum)){
	$pageNum = 1;
}
if(empty($noPayOrderId)){
	$pageSize = 12;
}else{
	$pageSize = 8;
	
}
$offset = ($pageNum-1)*$pageSize;

@$goodsName = $_REQUEST['searchGoods'];
@$typeId = $_REQUEST['searchType'];
$sql = "select goods.id as gId,type.id as tId,imgUrl,goods.name as gName,type.name as tName,stockPrice,sellPrice,sellStatus,basicStatus,stock,typeId from goods join type on goods.typeId=type.id";
$sql2 = " where stock>5 order by goods.id asc limit $offset,$pageSize";
$sql3 = " and stock>5 order by goods.id asc limit $offset,$pageSize";
$where = null;
$array = array();
if(!empty($goodsName)){
	$where = " where goods.name like concat('%',:goodsName,'%')";
	$array['goodsName'] = $goodsName;
}
if(!empty($typeId)){
	if(!empty($where)){
		$where .= " and typeId=:typeId";
	}else{
		$where = " where typeId=:typeId";
	}
	$array['typeId'] = $typeId;
}
$sql .= $where;
@$searchCount = dbSelectAll($sql,$array);
if(!empty($goodsName) || !empty($typeId)){
	$sql .= $sql3;
}else{
	$sql .= $sql2;
}
@$goods = dbSelectAll($sql,$array);
$num = ceil(count($searchCount)/$pageSize);
?>
   		<div class='container'>
   			<div class='row'>
   				<?php if(count($noPayOrders) > 0){?>
	   				<div class='col-xs-2'>
   				<?php }else{?>
   					<div style='display:none;'>
   				<?php }?>
   					<div class='panel panel-primary'>
   						<div class='panel-heading'>
   							<div class='panel-title'>当前订单</div>
   						</div>
   						<div class='list-group'>
   							<?php foreach($noPayOrders as $noPayOrder){?>
   								<div class='list-group-item'>
   									<a href='?noPayOrderId=<?php echo $noPayOrder['id'];?>&&searchGoods=<?php echo empty($goodsName)?'':$goodsName;?>&&searchType=<?php echo empty($typeId)?'':$typeId;?>' id='orderName<?php echo $noPayOrder['id'];?>'><?php echo $noPayOrder['name'];?></a>
   								</div>
   							<?php }?>
   						</div>
   					</div>
   				</div>
   				<?php if(count($noPayOrders) > 0){
   					@$goodsId = $_REQUEST['goodsId'];
   					$sql = "select goods.id as gId,`order`.id as oId,goods.name as gName,`order`.name as oName,imgUrl,sellPrice,goodsPrice,`count` 
   							from goods_order join goods on goods_order.goodsId=goods.id join `order` on goods_order.orderId=`order`.id where goods_order.orderId=:id";
   					@$noPayGO = dbSelectAll($sql,array('id'=>$noPayOrderId));
   					@$noPayGOonce = dbSelect($sql,array('id'=>$noPayOrderId));
   					@$noGoodsOrder = dbSelect('select id,name from `order` where id=:id',array('id'=>$noPayOrderId));
   					if(!empty($mes)){?>
   						<div class='col-xs-10'>
   							<div class='alert alert-danger'>
   								<?php @print($mes);?>
   								<a class='close' data-dismiss='alert'>&times;</a>
   							</div>
   						</div>
   				<?php }?>
	   				<div class='col-xs-10 orderCon' style='display:<?php echo empty($noPayOrderId)?'none':'block';?>;'>
	   					<div class='panel panel-primary'>
	   						<div class='panel-heading'>
	   							<div class='panel-title'>
	   								<?php if(!empty($noPayGO)){?>
		   								<a href='#title' data-toggle='collapse'>[<?php echo $noPayGOonce['oName'];?>]</a>已选商品
		   								<div class='col-xs-2 pull-right'>
		   									<a href='#payA<?php echo $noPayOrderId;?>' class='btn btn-success' id='payA' data-toggle='modal'>
		   										结算<span class='badge'><?php echo $noPayGOonce['goodsPrice'];?>元</span> 
		   									</a>
		   									<div class='modal' id='payA<?php echo $noPayOrderId;?>'>
		   										<div class='modal-dialog'>
		   											<div class='modal-content'>
		   												<div class='modal-header'>
		   													<h4 class='modal-title billTable'>
		   														订单[<?php echo $noPayGOonce['oName'];?>]结算清单
		   													</h4>
		   												</div>
		   												<div class='modal-body'>
			   												<table class='table table-bordered billTable'>
				   												<tr>
				   													<th>图片</th>
				   													<th>名称</th>
				   													<th>单价</th>
				   													<th>数量</th>
				   													<th>总价</th>
				   												</tr>
				   												<?php foreach($noPayGO as $data){
				   													@$price = $data['count']*$data['sellPrice'];
				   												?>
				   													<tr>
				   														<td><img src='img/<?php echo $data['imgUrl'];?>' class='goodsImg' /></td>
				   														<td><?php echo $data['gName'];?></td>
				   														<td><?php echo $data['sellPrice'];?></td>
				   														<td><?php echo $data['count'];?></td>
				   														<td><?php echo $price;?></td>
				   													</tr>
				   												<?php }?>
				   												<tr>
				   													<td colspan='4'>所有商品总价</td>
				   													<td><?php echo $noPayGOonce['goodsPrice'];?></td>
				   												</tr>
				   											</table>
				   										</div>
				   										<div class='modal-footer'>
				   											<a href='#payType<?php echo $noPayOrderId;?>' class='btn btn-success' data-toggle='modal' data-dismiss='modal'>结&nbsp;&nbsp;&nbsp;账</a>
				   											<button class='btn btn-primary' data-dismiss='modal'>再选选</button>
				   										</div>
		   											</div>
		   										</div>
		   									</div>
		   								</div>
	   								<?php }else{?>
	   									[<?php echo $noGoodsOrder['name'];?>]已选商品
	   								<?php }?>
	   							</div>
	   						</div>
	   						<div class="panel-body panel-collapse collapse <?php echo empty($noPayOrderId)?'':'in'?>" id='title'>
	   							<?php if(!empty($noPayGO)){
	   								foreach ($noPayGO as $data){
	   								$price = $data['count']*$data['sellPrice'];
	   								?>
		   								<div class='col-xs-2 col2<?php echo $data['gId'];?>'>
			   								<div class='thumbnail goods_orderThum<?php echo $data['gId'];?>'>
			   									<img id='orderImg' src='img/<?php echo $data['imgUrl'];?>' />
			   									<div>名称：<?php echo $data['gName'];?></div>
			   								</div>
			   								<div class='thumbnail goods_orderThum2<?php echo $data['gId'];?>'>
			   									<img id='orderImg' src='img/<?php echo $data['imgUrl'];?>' />
			   									<div>名称：<?php echo $data['gName'];?></div>
			   									<div class='goodsCaption goodsCaption<?php echo $data['gId'];?>'>
				   									<div>数量：<?php echo $data['count'];?></div>
				   									<div>单价：<?php echo $data['sellPrice'];?></div>
				   									<div>总价：<?php echo $price;?></div>
			   									</div>
			   									<a href='cancelGoods.php?goodsId=<?php echo$data['gId'];?>&&noPayOrderId=<?php echo $noPayOrderId;?>'>取消</a>
			   								</div>
		   								</div>
		   							<?php }?>
	   							<?php }else{?>
	   								你还没有选择商品
	   							<?php }?>
	   						</div>
	   					</div>
	   				</div>
	   				<div class='col-xs-10 pull-right'>
   				<?php }else{?>
   					<div class='col-xs-12 decide'>
   				<?php }?>
   					<div class='panel panel-primary'>
   						<div class='panel-heading'>
   							<div class='row'>
   								<div class='col-xs-3'>
		   							<div class='panel-title' style='margin-top:7px;'>所有商品</div>
   								</div>
   								<div class='col-xs-4 pull-right'>
		   							<form id='searchForm'>
		   								<div class='input-group'>
		   									<input class='form-control' placeholder='名称' id='searchGoods' name='searchGoods' value='<?php echo empty($goodsName)?'':$goodsName;?>' />
		   									<div class='input-group-btn'>
		   										<select id='typeSelect' class='btn btn-default' name='searchType' style='height:34px;'>
		   											<option value=''>全部</option>
		   											<?php foreach($types as $type){?>
		   												<option value='<?php echo $type['id']?>' <?php echo $type['id']==$typeId?"selected='selected'":'';?>><?php echo $type['name'];?></option>
		   											<?php }?>
		   										</select>
		   										<button class='btn btn-default' id='searchBtn' type='submit'><span class='glyphicon glyphicon-search'></span></button>
		   										<input type='hidden' value="<?php echo empty($noPayOrderId)?'':$noPayOrderId;?>" name='noPayOrderId' />
		   									</div>
		   								</div>
		   							</form>
   								</div>
   							</div>
   						</div>
   						<div class='panel-body'>
   							<div class='row'>
   								<?php foreach($goods as $good){
   									if(!empty($noPayOrderId)){?>
		   								<div class='col-xs-3'>
   									<?php }else{?>
   										<div class='col-xs-2'>
   									<?php }?>
			   							<div class='thumbnail' title='<?php echo $good['gName'];?>&#10;你值得拥有&#10;只要<?php echo $good['sellPrice'];?>!!' id='indexGoodsThum'>
			   								<img id='img' src='img/<?php echo $good['imgUrl'];?>' />
			   								<div id='name'><span class='name'><?php echo $good['gName'];?></span></div>
			   								<div id='price' style='color:red;'>¥ <span class='price'><?php echo $good['sellPrice'];?></span></div>
				   							<?php if(!empty($noPayOrderId)){?>
				   								<form action='addGoodsForOrder.php' method='post'>
					   								<div>
					   									<span id='countSpan'>数量：</span><input type='number' class='countInput' id='countInput<?php echo $good['gId'];?>' value='1' name='countInput' />
					   								</div>
						   							<button class='btn btn-success' id='addGoodsForOrder<?php echo $good['gId'];?>'>加入订单</button>
					   								<input type='hidden' value='<?php echo $noPayOrderId;?>' name='noPayOrderId' />
					   								<input type='hidden' value='<?php echo $good['gId'];?>' name='goodsId' />
				   								</form>
				   							<?php }?>
			   							</div>
	   								</div>
   								<?php }?>
   							</div>
   							<div class='row'>
   								<div class='col-xs-12 text-center'>
   									<a href='?pageNum=<?php echo "1";?>&&searchGoods=<?php echo empty($goodsName)?'':$goodsName;?>&&searchType=<?php echo empty($typeId)?'':$typeId;?>&&noPayOrderId=<?php echo empty($noPayOrderId)?'':$noPayOrderId;?>' id='firstBtn' class='btn btn-default'>首页</a>
   									<?php if($pageNum <= 1){?>
   										上一页
   									<?php }else{?>
		   								<a href='?pageNum=<?php echo $pageNum -1;?>&&searchGoods=<?php echo empty($goodsName)?'':$goodsName;?>&&searchType=<?php echo empty($typeId)?'':$typeId;?>&&noPayOrderId=<?php echo empty($noPayOrderId)?'':$noPayOrderId;?>' id='prevBtn' class='btn btn-default'>上一页</a>
   									<?php }?>
   									<?php if($pageNum < $num){?>
		   								<a href='?pageNum=<?php echo $pageNum +1;?>&&searchGoods=<?php echo empty($goodsName)?'':$goodsName;?>&&searchType=<?php echo empty($typeId)?'':$typeId;?>&&noPayOrderId=<?php echo empty($noPayOrderId)?'':$noPayOrderId;?>' id='nextBtn' class='btn btn-default'>下一页</a>
   									<?php }else{?>
   										下一页
   									<?php }?>
	   								<a href='?pageNum=<?php echo $num;?>&&searchGoods=<?php echo empty($goodsName)?'':$goodsName;?>&&searchType=<?php echo empty($typeId)?'':$typeId;?>&&noPayOrderId=<?php echo empty($noPayOrderId)?'':$noPayOrderId;?>' id='lastBtn' class='btn btn-default'>末页</a>
   								</div>
   							</div>
   						</div>
   					</div>
   				</div>
   			</div>
   		</div>
   		<div class='modal' id='payType<?php echo $noPayOrderId;?>'>
   			<div class='modal-dialog'>
   				<div class='modal-content'>
   					<div class='modal-header'>
   						<h4 class='modal-title'>
   							订单[<?php echo $noPayGOonce['oName'];?>]结算方式
   						</h4>
   					</div>
   					<div class='modal-body payModal'>
   						<img src='img/pay/zhifubao.jpg' />
   						<a href='#payPage<?php echo $noPayOrderId;?>' data-toggle='modal' data-dismiss='modal'>支付宝支付</a>
   						<img src='img/pay/wangyin.jpg' />
   						<a href='#'>网银支付</a>
   						<img src='img/pay/xinyongka.jpg' />
   						<a href='#'>信用卡支付</a>
   						<img src='img/pay/daifu.png' />
   						<a href='#'>找人代付</a>
   					</div>
   					<div class='modal-footer'>
   						<button class='btn btn-default' data-dismiss='modal'>返回</button>
   					</div>
   				</div>
   			</div>
   		</div>
   		<div class='modal' id='payPage<?php echo $noPayOrderId;?>'>
   			<div class='modal-dialog' style='width:400px;'>
   				<div class='modal-content'>
   					<div class='modal-header'>
   						<h4 class='modal-title'>
   							订单[<?php echo $noPayGOonce['oName'];?>]结算
   						</h4>
   					</div>
   					<form class='form-horizontal' action='payOrder.php?noPayOrderId=<?php echo $noPayOrderId;?>' method='post'>
	   					<div class='modal-body'>
	   						<div class='form-group'>
	   							<label class='col-xs-4 control-label' for='payName'>收件人姓名</label>
	   							<div class='col-xs-8'>
	   								<input name='payName' class='form-control' type='text' id='payName' required='required' />
	   							</div>
	   						</div>
	   						<div class='form-group'>
	   							<label class='col-xs-4 control-label' for='payLocation'>收件地址</label>
	   							<div class='col-xs-8'>
	   								<input name='payLocation' class='form-control' type='text' id='payLocation' required='required' />
	   							</div>
	   						</div>
	   						<div class='form-group'>
	   							<label class='col-xs-4 control-label' for='phone'>联系电话</label>
	   							<div class='col-xs-8'>
	   								<input name='phone' class='form-control' type='number' id='phone' required='required' />
	   							</div>
	   						</div>
	   						<div class='form-group'>
	   							<label class='col-xs-4 control-label' for='payPrice'>应付金额</label>
	   							<div class='col-xs-8'>
	   								<input value='<?php echo $noPayGOonce['goodsPrice'];?>' class='form-control' disabled='disabled' />
	   							</div>
	   						</div>
	   						<div class="form-group <?php echo @mes=='付款失败，请仔细比对金额'?'has-error':'';?>">
	   							<label class='col-xs-4 control-label' for='payPrice'>付款金额</label>
	   							<div class='col-xs-8'>
	   								<input name='payPrice' class='form-control' type='number' id='payPrice' required='required' />
	   							</div>
	   						</div>
	   						<div class='form-group'>
	   								<div class='col-xs-4 col-xs-offset-4'>
		   								<img src='img.php' class='codeImg' />
	   								</div>
	   								<a href='#' class='nextCode'>看不清，换一张</a>
	   							</div>
	   							<div class='form-group <?php echo @$mes=="验证码错误"?'has-error':'';?>'>
	   								<label class='col-xs-4 control-label' for='code'>验证码</label>
	   								<div class='col-xs-8'>
	   									<div class='input-group'>
	   										<div class='input-group-addon'>
		   										<span class='glyphicon glyphicon-retweet'></span>
	   										</div>
		   									<input class='form-control' class='form-control' name='code' id='code' placeholder='验证码' required='required' />
	   									</div>
	   								</div>
	   							</div>
	   					</div>
	   					<div class='modal-footer'>
	   						<button class='btn btn-success' type='submit'>确定付款</button>
	   						<a href='index.php' class='btn btn-default' data-dismiss='modal'>返回主页</a>
	   					</div>
   					</form>
   				</div>
   			</div>
   		</div>
   		<script src='bootstrap/js/tz_fly.js'></script>
   		<script src='bootstrap/js/nextImg.js'></script>
   		<script>
   			<?php if(!empty($noPayGO)){
				foreach ($noPayGO as $data){
			?>
				$('.goods_orderThum2<?php echo $data['gId'];?>').css({'position':'absolute','left':0,'top':0,'opacity':'0','z-index':'5'});
				$('.goods_orderThum2<?php echo $data['gId'];?>').hover(function(){
					$(this).css({'opacity':'1'});
				},function(){
					$(this).css({'opacity':'0'});
				});
				<?php }?>
			<?php }?>
			var mes = "<?php echo @$status;?>";
			if(mes == "ok"){
				$.message('消息提示','付款成功，牢记你的订单信息');
			}
   		</script>
   	</body>
</html>



